<?php
/**
 * @Author: Zhao Yang[873777808@qq.com]
 * @Date:   2015-03-03 18:32:47
 * @Last Modified time: 2015-04-23 14:42:31
 */
namespace Common\Model;
use Think\Model\RelationModel;

class AdminRelationModel extends RelationModel{
	protected $tableName = 'admin';
	protected $_link = array(
		'auth_group' => array(
				'mapping_fields' =>'id,title',
				'mapping_type'      =>  self::MANY_TO_MANY,
				'foreign_key'       =>  'uid',
				'relation_foreign_key'  =>  'group_id',
				'relation_table'    =>  '__AUTH_GROUP_ACCESS__',
				'mapping_order' =>'uid',
				'mapping_limit'=>1
			)
	);


	protected $_validate = array(
		array('name','require','用户名不能为空！'),
		array('name','1,20','用户名20字符以内！',0,'length'),
		array('name','','帐号名称已经存在！',0,'unique',1), 
		array('pwd','require','密码不能不空','','',1),
		array('pwd','checkPwd','密码格式错误,8-20位,不能全为数字',2,'callback'),
		array('pwdc','pwd','两次密码不一致',0,'confirm'),
		array('mail','require','邮箱不能为空'),
		array('mail','email','邮箱格式错误'),
	);



	public function checkPwd()
	{
		$var = I('post.pwd');
		if(strlen($var)<8||strlen($var)>20){
			return false;
		}
		if(preg_match('/^\d+$/', $var)){
			return false;
		}
		return true;
	}

	// 自动完成
	protected $_auto = array(
		// 1=新增 2=更新 3=所有情况
		array('pwd','_code',3,'callback')
	);

	public function _code()
	{
		$code = I('post.code');
		if(I('post.pwd')){
			return md5(I('post.pwd').$code);
		}else{
			return false;
		}
	}
}